package com.douxg.learning.springboot.scheduled;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.concurrent.*;

/**
 * @ClassName job1
 * @Description TODO
 * @Author douxg
 * @Date 2019/12/12 14:30
 * @Version 1.0
 */
@Configuration
@EnableScheduling
public class job1 {
    private static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 3, 1, TimeUnit.MINUTES, new LinkedBlockingQueue<>(100), new ThreadFactory() {
        private int i = 0;

        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r, "pool_StatisActionJob_" + (++i));
            return thread;
        }
    }, new ThreadPoolExecutor.CallerRunsPolicy());
    @Scheduled(fixedDelayString = "10")
    public void run() throws Exception {
        for (int i = 0; i < 104; i++) {
            final int j = i;
            Future<Integer> submit = threadPoolExecutor.submit(new Callable<Integer>() {
                @Override
                public Integer call() throws Exception {
                    TimeUnit.SECONDS.sleep(3);
                    System.out.println(Thread.currentThread().getName() + "*******" + j);
                    return j;
                }
            });
        }
        System.out.println("任务发放完毕");
    }
}
